/**
 * Candidate number: 35834
 */
package tv.starfu.sequencer.model;

import com.artemis.Component;
import com.artemis.Entity;
import com.artemis.World;
import java.util.ArrayList;
import net.beadsproject.beads.core.AudioContext;
import tv.starfu.game.EntityFactory;
import tv.starfu.game.EntityType;
import tv.starfu.sequencer.components.AmpEnvelope;
import tv.starfu.sequencer.components.PitchEnvelope;
import tv.starfu.sequencer.components.SamplePlayback;
import tv.starfu.sequencer.components.TimeVector;

/**
 * A class which reads up to 8 channels of data into one drum hit.
 * @author niadzi
 */

        // channels[0] -> on/off
        // channels[1] -> pitch
        // channels[2] -> velocity
        // channels[3] -> duration
        // channels[4] -> shuffle
        // channels[5] -> pitchbend
        // channels[6] -> glitch
        // channels[7] -> reverse

public class DrumHit extends Instrument {
    
    private TimeVector      tv          = new TimeVector();
    private SamplePlayback  sample      = new SamplePlayback();
    private AmpEnvelope     ampEnv      = new AmpEnvelope();
    private PitchEnvelope   pitchEnv    = new PitchEnvelope();
   
    private int             numHits     = 1;
    private int             bar;
    private String          drum;
    
    private final AudioContext ac;
    private World world;
    
    /**
     * A drum Hit Factory.
     * @param audiocontext ac
     * @param world world
     */
    public DrumHit(AudioContext ac, World world, String drumName, int bar) {
        this.ac = ac;
        this.world = world;
        this.drum = drumName;
        ampEnv.setAc(ac);
        pitchEnv.setAc(ac);
        this.bar = bar;
    }
    
    /**
     * Creates a new drum hit entity.
     * @param channels  all channels from one step of the pattern
     * @param step      global step
     * @param sampleNo  sample number
     */
    public void createHit(int[] channels, int step, int sampleNo) {
 
        /* Turn ON! */
        if(channels[0] != 0) { sample.setSampleIndex(sampleNo); 
        
        /* Set pitch sustain value */
        if(channels[1] != 0) { }//pitchEnv.setSusVal((float)channels[1]); }
        
        /* Set velocity sustain value */
        if(channels[2] != 0) { }// ampEnv.setSusVal(channels[2]/1f); }
        
        /* Set velocity sustain time */
        if(channels[3] != 0) { }// ampEnv.setSustain_ms(channels[4]*50); }
        
        /* Set shuffle */
        if(channels[4] != 0) {  }
        
        /* Set pitchbend */
        if(channels[5] != 0) {  }
        
        /* Set glitch */
        if(channels[6] != 0) {  }
        
        /* Set reverse */
        if(channels[7] != 0) {  }
        
        ampEnv.createEnvelope();
        pitchEnv.createEnvelope();
        sample.setSampleGroup(drum);
        
        Entity e = EntityFactory.createDrumHit(world, ac, new TimeVector(bar,step,0), ampEnv, pitchEnv, sample);
        e.refresh();
        }
    }

    public String getDrum() {
        return drum;
    }

    public void setDrum(String drum) {
        this.drum = drum;
    }

    
}
